package com.rt.easyjava.common.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.rt.easyjava.common.utils.others.StringUtils;
import io.swagger.annotations.ApiModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wxy on 2017/9/12.
 */
@ApiModel(description = "分页传输对象")
public class CommonDto implements Serializable {
    private static final long serialVersionUID = 1L;

    @JsonIgnore
    private int page;
    @JsonIgnore
    private int limit = 10; //default value 10
    @JsonIgnore
    private String sort; // +id, -id
    @JsonIgnore
    private Boolean hasFooter = true;
    @JsonIgnore
    private Boolean isAsc = true;
    @JsonIgnore
    private String column = "id";

    /**
     * controller中 queryWrapper用法：
     *         for (PageDto sortDto:dto.getSortList()){
     *             queryWrapper.orderBy(true, sortDto.getAsc(), sortDto.getColumn());
     *         }
     * @return
     */
    public List<CommonDto> getSortList() {
        List<CommonDto> sortList = new ArrayList<>();
        if(!StringUtils.isNull(this.sort)){
            List<String> sList = Arrays.asList(sort.split(","));
            for (String sort1:sList) {
                CommonDto sortDto = new CommonDto();
                char c = sort1.charAt(0);
                if(c=='+'){
                    sortDto.setAsc(true);
                }else{
                    sortDto.setAsc(false);
                }
                sortDto.setColumn(sort1.substring(1));
                sortList.add(sortDto);
            }
        }
        return sortList;
    }

    @JsonIgnore
    public boolean getCondition(){
        /*if(sort==null || "".equalsIgnoreCase(sort)){
            return false;
        }*/
        return true;
    }

    @JsonIgnore
    public Boolean isAsc() {
        if(!StringUtils.isNull(sort)){
            char c = sort.charAt(0);
            if(c=='+'){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    @JsonIgnore
    public String getColumn() {
        if(!StringUtils.isNull(sort)){
            return sort.substring(1);
        }
        return "id";
    }

    @JsonIgnore
    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    @JsonIgnore
    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    @JsonIgnore
    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public Boolean getHasFooter() {
        return hasFooter;
    }

    public void setHasFooter(Boolean hasFooter) {
        this.hasFooter = hasFooter;
    }

    public Boolean getAsc() {
        return isAsc;
    }

    public void setAsc(Boolean asc) {
        isAsc = asc;
    }

    public void setColumn(String column) {
        this.column = column;
    }
}
